package com.fxsh168.server.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.fxsh168.server.utils.CountDownTimerUtils;
import com.fxsh168.server.utils.MD5Utils;
import com.fxsh168.server.utils.RegExp;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;


public class ReGinsterActivity extends BaseActivity implements View.OnClickListener {

    private EditText mPutPhone, mYao;
    private EditText mPutYanzhengma, mPutPassword, mPutPassAgin;
    private TextView mGetYanzhengma, mShengMingg;
    private String mPhone, mPhone2, mYanzhengma, mYZM, mPassword, jMPassword;
    Button mNext;
    RelativeLayout reAgrenn;
    ImageView imAgreen;
    boolean isAgreen = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reginster);

        mShengMingg = (TextView) findViewById(R.id.teShengMing);
        mPutPhone = (EditText) findViewById(R.id.putPhone);
        mPutYanzhengma = (EditText) findViewById(R.id.putYanzhengma);
        mGetYanzhengma = (TextView) findViewById(R.id.getYanzhengma);
        mPutPassword = (EditText) findViewById(R.id.putPassword);
        mPutPassAgin = (EditText) findViewById(R.id.mPutPassAgin);
//        mYao = (EditText) findViewById(R.id.mYao);
        mNext = (Button) findViewById(R.id.mNext);
        reAgrenn = (RelativeLayout) findViewById(R.id.reAgrenn);
        imAgreen = (ImageView) findViewById(R.id.imAgreen);
        mGetYanzhengma.setOnClickListener(this);
        mNext.setOnClickListener(this);
        mShengMingg.setOnClickListener(this);
        reAgrenn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.getYanzhengma:
                getYanzhengma();
                break;
            case R.id.mNext:
                if (isAgreen) {
                    btnRegistering();
                } else {
                    showToast("同意法律声明与隐私政策才能注册");
                }
                break;
            case R.id.teShengMing:
                jumpActivity(ShengMingActivity.class);
                break;
            case R.id.reAgrenn:
                if (isAgreen) {
                    imAgreen.setImageResource(R.mipmap.weidianji);
                    isAgreen = false;
                } else {
                    imAgreen.setImageResource(R.mipmap.dianji);
                    isAgreen = true;
                }
                break;
        }
    }


    public void getYanzhengma() {
        //获取输入框信息
        mPhone = mPutPhone.getText().toString().trim();
        mPassword = mPutPassword.getText().toString().trim();
        if ((!TextUtils.isEmpty(mPhone)) && mPhone != null ) {
            OkHttpUtils.post().url(Constant.REGIYZM).addParams("phone", mPhone)
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    showToast("连接不上服务器!");
                }

                @Override
                public void onResponse(String response, int id) {
                    if (response != null) {
                        RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                        Object data = regiYZMCG.getData();
                        if (data instanceof String) {
                            mYZM = data.toString().trim();
                            String code = regiYZMCG.getCode();
                            if (code.equals(200 + "")) {
                                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(mGetYanzhengma, 60000, 1000);
                                countDownTimerUtils.start();
                                showToast("恭喜您获取验证码成功!");
                            }
                        } else if (data instanceof List<?>) {
                            String code = regiYZMCG.getCode();
                            if (code.equals(201 + "")) {
                                showToast("请输入11位电话号码!");
                            } else if (code.equals(202 + "")) {
                                showToast("发送失败!");
                            } else if (code.equals(203 + "")) {
                                showToast("账号已存在，请登陆!");
                                finish();
                            }
                        }
                    }
                }
            });
        } else if ((TextUtils.isEmpty(mPhone)) || mPhone == null) {
            mPutPhone.setError("手机号不能为空!");
        }  else if (mPhone.length() != 11) {
            mPutPhone.setError("请输入正确的手机号码!");
        }
    }

    public void btnRegistering() {
        mYanzhengma = mPutYanzhengma.getText().toString().trim();
        mPhone2 = mPutPhone.getText().toString().trim();
//        mPhone2 = mPhone;
        mPassword = mPutPassword.getText().toString().trim();
//        String mYaoText = mYao.getText().toString().trim();
        //密码加密
        jMPassword = MD5Utils.MD532(mPassword);
        Log.e("tag", "===" + mPhone2 + "===" + mYZM + "===" + mYanzhengma + "===" + mPassword);
        if ((!(TextUtils.isEmpty(mPhone2))) && mPhone2 != null) {
            if (/*mYZM.equals(mYanzhengma)) && */!TextUtils.isEmpty(mYanzhengma)) {
                if ((mPassword != null && !TextUtils.isEmpty(mPassword)) && RegExp.checkPassword(mPassword)) {
                    Intent intent = new Intent(ReGinsterActivity.this, ReginNameActivity.class);
                    intent.putExtra("phone", mPhone2);
                    intent.putExtra("mPass", jMPassword);
//                    intent.putExtra("yao", mYaoText);
                    startActivity(intent);
                } else {
                    mPutPassword.setError("请输入6-18位字符或数字密码!");
                }
            } else {
                mPutYanzhengma.setError("请输入验证码!");
            }
        } else {
            mPutPhone.setError("请输入11位正确的手机号!");
        }
        Log.e("chenggongmei",mPhone+"");
    }


    public void Re() {
        OkHttpUtils.post().addParams("phone", mPhone2).addParams("password", jMPassword)
                .url(Constant.REGISTER).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(ReGinsterActivity.this, "连接不上服务器!", Toast.LENGTH_SHORT).show();
                mPutPhone.getText().clear();
//                mPutYanzhengma.getText().clear();
                mPutPassword.getText().clear();
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                    Object data = regiYZMCG.getData();
                    if (data instanceof String) {
                        String code = regiYZMCG.getCode().trim();
                        if (code.equals(200 + "")) {
                            Toast.makeText(ReGinsterActivity.this, "恭喜您，注册成功!", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(ReGinsterActivity.this, LoginActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("phone", mPhone2);
                            bundle.putString("password", mPassword);
                            intent.putExtra("regiBundle", bundle);
                            ReGinsterActivity.this.setResult(Constant.REGISRESPONDCODE, intent);
                            ReGinsterActivity.this.finish();
                        }
                    } else if (data instanceof List<?>) {
                        String code = regiYZMCG.getCode().trim();
                        if (code.equals(201 + "")) {
                            Toast.makeText(ReGinsterActivity.this, "注册失败!", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            }
        });
    }

    public void btnAdd(View view) {
        jumpActivity(ReginPhotoActivity.class);
    }
}

